package com.leyou.personnel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.common.pojo.Result;
import com.leyou.common.utils.StringUtils;
import com.leyou.personnel.data.dto.AdministratorsAuthorityDto;
import com.leyou.personnel.data.dto.AdministratorsRoleDto;
import com.leyou.personnel.data.entity.AdministratorsRole;
import com.leyou.personnel.data.entity.AdministratorsRoleAuthority;
import com.leyou.personnel.data.vo.RoleListShowSearchVo;
import com.leyou.personnel.mapper.AdministratorsRoleAuthorityMapper;
import com.leyou.personnel.mapper.AdministratorsRoleMapper;
import com.leyou.personnel.service.AdministratorsRoleAuthorityService;
import com.leyou.personnel.service.AdministratorsRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xhh
 * @since 2022-05-18
 */
@Service
public class AdministratorsRoleServiceImpl extends ServiceImpl<AdministratorsRoleMapper, AdministratorsRole> implements AdministratorsRoleService {
    @Autowired
    private AdministratorsRoleMapper roleMapper;
    @Autowired
    private AdministratorsRoleAuthorityMapper roleAuthorityMapper;
    @Autowired
    private AdministratorsRoleAuthorityService roleAuthorityService;
    @Override
    public Result roleList() {
        Result<List<AdministratorsRole>> result=new Result();
        List<AdministratorsRole> list = this.list();
        if(list!=null){
            result.setCode("200");
            result.setMsg("成功");
            result.setResult(Result.RESULT_FLG.SUCCESS.toString());
            result.setData(list);
        }else{
            result.setCode("403");
            result.setMsg("失败");
            result.setResult(Result.RESULT_FLG.FAIL.toString());
        }
        return result;
    }

    @Override
    public Result roleListShow(RoleListShowSearchVo vo) {
        Result<Page<AdministratorsRole>> result=new Result();
        QueryWrapper queryWrapper=new QueryWrapper();
        if(!vo.getRoleName().equals("") && vo.getRoleName()!=null) {
            queryWrapper.eq("role_name", vo.getRoleName());
        }
        Page page=new Page(vo.getPage(),vo.getLimit());
        Page<AdministratorsRole> pageAdministratorsRoleList = this.page(page, queryWrapper);
        if(pageAdministratorsRoleList.getRecords()!=null){
            result.setCode("200");
            result.setMsg("成功");
            result.setResult(Result.RESULT_FLG.SUCCESS.toString());
            result.setData(pageAdministratorsRoleList);
        }else{
            result.setCode("403");
            result.setMsg("失败");
            result.setResult(Result.RESULT_FLG.FAIL.toString());
        }
        return result;
    }

    @Override
    @Transactional
    public Result roleInsert(AdministratorsRoleDto administratorsRoleDto) {
        Result result=new Result();

        AdministratorsRole role=new AdministratorsRole();
        role.setRoleName(administratorsRoleDto.getRoleName());
        String roleNumber = generateRoleNumber();
        role.setRoleNumber(roleNumber);

        //添加到 administrators_role 表中
        boolean save = this.save(role);

        List<AdministratorsAuthorityDto> administratorsAuthorityDto = administratorsRoleDto.getAdministratorsAuthorityDto();
        List<AdministratorsRoleAuthority> list=new ArrayList<>();
        for (AdministratorsAuthorityDto dto : administratorsAuthorityDto) {
            AdministratorsRoleAuthority administratorsRoleAuthority=new AdministratorsRoleAuthority();
            administratorsRoleAuthority.setRoleNumber(roleNumber);
            administratorsRoleAuthority.setAuthorityNumber(dto.getAuthorityNumber());
            list.add(administratorsRoleAuthority);
        }
        boolean saveBatch = roleAuthorityService.saveBatch(list);
        if(save && saveBatch){
            result.setCode("200");
            result.setMsg("成功");
            result.setResult(Result.RESULT_FLG.SUCCESS.toString());
        }else{
            result.setCode("403");
            result.setMsg("失败");
            result.setResult(Result.RESULT_FLG.FAIL.toString());
        }
        return result;
    }

    @Override
    public Result roleDel(List<String> list) {
        Result<List<String>> result=new Result();
        QueryWrapper queryWrapper=new QueryWrapper();
        int flag=0;
        //记录未删除的管理员账号
        List<String> roleNumbersNotDelete=new ArrayList<>();
        for (String managerAccountNumber : list) {
            queryWrapper.clear();
            queryWrapper.eq("manager_account_number",managerAccountNumber);
            int delete = roleMapper.delete(queryWrapper);
            if(delete>0){
                flag++;
            }else{
                //记录未删除的管理员账号
                roleNumbersNotDelete.add(managerAccountNumber);
            }
        }
        if(flag==list.size()){
            result.setCode("200");
            result.setResult(Result.RESULT_FLG.SUCCESS.getValue());
            result.setData(null);
            result.setMsg("全部删除失败");
        }else{
            result.setCode("403");
            result.setResult(Result.RESULT_FLG.FAIL.getValue());
            result.setData(null);
            result.setMsg("删除失败");
            //将未删除成功的账号添加到data中
            result.setData(roleNumbersNotDelete);
        }
        return result;
    }

    @Override
    public Result roleEmpowerEcho(String roleNumber) {



        return null;
    }

    @Override
    public Result roleEmpowerModify(AdministratorsRoleDto administratorsRoleDto) {
        return null;
    }

    /**
     * 生成roleNumber
     * @author: xhh
     * @date: 2022/5/26
     * @description: 找出最大的role_number，并+1转化成 例如006的格式
     * @return:
     * @params:
     */
    private String generateRoleNumber(){
        List<AdministratorsRole> list = this.list();
        int max=0;
        for (AdministratorsRole administratorsRole : list) {
            String roleNumber = administratorsRole.getRoleNumber();
            roleNumber.replaceAll("0","");
            Integer roleNum=Integer.parseInt(roleNumber);
            if(roleNum>max){
                max=roleNum;
            }

        }
        //生成的roleNumber
        StringBuffer roleNumber = StringUtils.codedNumber(max + 1, 3);
        return roleNumber.toString();
    }
}
